const tools = require('../utils/tools')

module.exports = class users_mod extends require('./model') {
  /**
   * 数据库登录
   * @param username
   * @param password
   * @param type
   * @returns {Promise<any>}
   * @constructor
   */
  // static loginUser(username, password, type) {
  //   type = Number(type)
  //   let sql = "select * 1from user where binary username='" + username + "' and password='" + password + "' and type= " + type
  //   return this.query(sql)
  // }

  static loginUser(username, password, type) {
    type = Number(type)
    return new Promise((resolve, reject) => {
      let sql = "select * from user where binary username='" + username + "' and password='" + password + "' and type= " + type
      this.query(sql).then(result => {
        resolve(result)
      }).catch(err => {
        // reject(new Error('登录失败'))
        reject('登录失败')
      })
    })
  }

  // 查找用户数据
  static findUser(type, id) {
    type = Number(type)
    return new Promise((resolve, reject) => {
      let sql = "select * from user where binary id=" + id + " and type= " + type
      this.queryOne(sql).then(result => {
        resolve(result)
      }).catch(err => {
        reject('获取用户数据失败')
      })
    })
  }

  // /**
  //  * 根据id登录
  //  * @param id
  //  * @param password
  //  * @param type
  //  * @constructor
  //  */
  // static LoginUserByid(id, password, type) {
  //   type = Number(type)
  //   return new Promise((resolve, reject) => {
  //     let sql = "select * from user where binary id='" + id + "' and password='" + password + "' and type= " + type
  //     // console.log(sql)
  //     this.query(sql).then(result => {
  //       resolve(result)
  //     }).catch(err => {
  //       reject('登录失败')
  //     })
  //   })
  // }
  /**
   * 根据用户类型进行用户信息获取(分页获取总数量与数据)
   * @param type
   * @param currPage
   * @param pageSize
   */
  static getUsersByTypePageMod(type, pageSize, currPage) {
    pageSize = Number(pageSize)
    currPage = Number(currPage)
    currPage = Number(currPage * pageSize)
    return new Promise((resolve, reject) => {
      let sql = 'select * from user where type = ' + type + ' order by id asc LIMIT ?,?'
      this.query(sql, this.formatParams(currPage, pageSize)).then(result => {
        resolve(result)
      }).catch(() => {
        reject('获取用户数据失败')
      })
    })
  }

  static getUsersByTypePageTotal(type) {
    return new Promise((resolve, reject) => {
      let sql = "select count(1) as count from user where type = " + type
      this.query(sql).then(result => {
        resolve(result)
      }).catch(() => {
        reject('获取用户总数失败')
      })
    })
  }
  // 分页获取搜索的用户数据
  static getUsersByTypeAndCharMod(type, inputText, charType, currPage, pageSize) {
    pageSize = Number(pageSize);
    currPage = Number(currPage);
    currPage = Number(currPage * pageSize)
    return new Promise((resolve, reject) => {
      let sql = 'select * from `user` where  ' + charType + '  like "%' + inputText + '%" and type = ' + type + ' order by modifytime desc LIMIT ?,?'
      this.query(sql, this.formatParams(currPage, pageSize)).then(result => {
        resolve(result)
      }).catch(err => {
        reject('查询失败')
      })
    })
  }

  static getUsersByTypeAndCharTotal(type, inputText, CharType) {
    return new Promise((resolve, reject) => {
      let sql = 'select count(1) as count from user where  ' + CharType + '  like "%' + inputText + '%" and type = ' + type
      console.log(sql)
      this.query(sql).then(result => {
        resolve(result)
      }).catch(err => {
        reject('获取总数失败')
      })
    })
  }
  /**
   * 获取所有用户信息
   */
  static getAllUsersData() {
    return new Promise((resolve, reject) => {
      let sql = 'select * from user '
      this.query(sql).then(result => {
        resolve(result)
      }).catch(err => {
        reject('获取所有用户数据失败')
      })
    })
  }

  /**
   * 将redis数据未重复的进行插入
   * @param inXlsxArr
   */
  static inXlsxData(inXlsxArr) {
    return new Promise((resolve, reject) => {
      for (let i = 0; i < inXlsxArr.length; i++) {
        let sql = "insert into user (id,username,password,head,address,sex,classes,type) values(" + inXlsxArr[i].id + ", '" + inXlsxArr[i].username + "' , '" + inXlsxArr[i].password + "', '" + inXlsxArr[i].head + "'," +
          "'" + inXlsxArr[i].address + "', '" + inXlsxArr[i].sex + "', '" + inXlsxArr[i].classes + "', '" + inXlsxArr[i].type + "')"
        this.query(sql).then(result => {
          resolve(result)
        }).catch(err => {
          reject('用户数据导入失败')
        })
      }
    })
  }

  /**
   * 删除用户表用户
   * @param id
   */
  static removeUserDataMod(id) {
    return new Promise((resolve, reject) => {
      const sql = "delete from user where id = " + id
      this.query(sql).then(result => {
        resolve('删除用户成功')
      }).catch(err => {
        reject('删除用户失败')
      })
    })
  }
  /**
   * 删除阅读表用户
   * @param id
   */
  static delRead(id) {
    return new Promise((resolve, reject) => {
      const sql = "delete from `read` where u_id = " + id
      this.query(sql).then(result => {
        resolve('删除阅读用户成功')
      }).catch(err => {
        reject('删除阅读用户失败')
      })
    })
  }

  /**
   * 管理员进行用户信息修改
   * @param u_id
   * @param username
   * @param sex
   * @param address
   * @param type
   */
  static editUserDataMod(u_id, username, sex, address, type) {
    // 获取当前时间
    let currTime = tools.getDate19()
    return new Promise((resolve, reject) => {
      let sql = "update `user` set username= '" + username + "', sex='" + sex + "' , address='" + address + "', type =" + type + " , modifytime= '" + currTime + "' where id=" + u_id
      this.query(sql).then(result => {
        resolve("更新成功")
      }).catch(err => {
        reject("可能是因为网络不好,更新失败")
      })
    })
  }

  /**
   *修改个人密码
   * @param u_id
   * @param oldpassword
   * @param newpassword
   */
  static upPwdMod(u_id, oldpassword, newpassword) {
    return new Promise((resolve, reject) => {
      let sql = 'update `user` set password = ? where password = ? and id = ?'
      this.query(sql, this.formatParams(newpassword, oldpassword, u_id)).then(result => {
        resolve(result)
      }).catch(() => {
        reject('更新失败')
      })
    })
  }

  /**
   * 修改个人头像
   * @param head_imgUrl
   * @param u_id
   */
  static upUserHeadMod(head_imgUrl, u_id) {
    return new Promise((resolve, reject) => {
      let sql = "update `user` set head= '" + head_imgUrl + "' where id = " + u_id
      this.query(sql).then(result => {
        resolve("上传成功")
      }).catch(() => {
        reject('上传失败')
      })
    })
  }

  // 获取新头像
  static getAvatarMod(u_id) {
    // console.log(u_id)
    return new Promise((resolve, reject) => {
      let sql = "select `head` from user where binary id = " + u_id
      this.query(sql).then(result => {
        resolve(result)
      }).catch(() => {
        reject('获取新头像失败')
      })
    })
  }
}